package com.neusoft.elm.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.util.DBUtil;

public class BusinessDaoImpl implements BusinessDao {

    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;

    @Override
    public List<Business> listBusiness(String businessName, String businessAddress) throws Exception {
        System.out.println("BusinessDaoImpl.listBusiness() 被调用");
        System.out.println("查询参数 - businessName: " + businessName + ", businessAddress: " + businessAddress);
        
        List<Business> list = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select * from business where 1=1");
        List<Object> params = new ArrayList<>();
        
        if (businessName != null && !businessName.trim().isEmpty()) {
            sql.append(" and businessName like ?");
            params.add("%" + businessName + "%");
        }
        if (businessAddress != null && !businessAddress.trim().isEmpty()) {
            sql.append(" and businessAddress like ?");
            params.add("%" + businessAddress + "%");
        }
        sql.append(" order by businessId");
        
        System.out.println("执行的SQL: " + sql.toString());
        System.out.println("SQL参数: " + params);
        
        try {
            con = DBUtil.getConnection();
            System.out.println("数据库连接成功");
            pst = con.prepareStatement(sql.toString());
            for (int i = 0; i < params.size(); i++) {
                pst.setObject(i + 1, params.get(i));
            }
            rs = pst.executeQuery();
            System.out.println("SQL执行成功，开始处理结果集");
            while (rs.next()) {
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                list.add(business);
                System.out.println("处理商家数据: " + business);
            }
            System.out.println("查询完成，共获取 " + list.size() + " 条记录");
        } catch (Exception e) {
            System.err.println("查询商家列表时发生异常: " + e.getMessage());
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs, pst);
            System.out.println("数据库资源已释放");
        }
        return list;
    }

    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws Exception {
        System.out.println("BusinessDaoImpl.listBusinessByOrderTypeId() 被调用, orderTypeId: " + orderTypeId);
        List<Business> list = new ArrayList<>();
        String sql = "select * from business where orderTypeId=? order by businessId";
        System.out.println("执行的SQL: " + sql);
        try {
            con = DBUtil.getConnection();
            System.out.println("数据库连接成功");
            pst = con.prepareStatement(sql);
            pst.setInt(1, orderTypeId);
            rs = pst.executeQuery();
            System.out.println("SQL执行成功，开始处理结果集");
            while (rs.next()) {
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                list.add(business);
                System.out.println("处理商家数据: " + business);
            }
            System.out.println("查询完成，共获取 " + list.size() + " 条记录");
        } catch (Exception e) {
            System.err.println("根据订单类型查询商家列表时发生异常: " + e.getMessage());
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs, pst);
            System.out.println("数据库资源已释放");
        }
        return list;
    }

    @Override
    public Business getBusinessById(Integer businessId) throws Exception {
        System.out.println("BusinessDaoImpl.getBusinessById() 被调用, businessId: " + businessId);
        Business business = null;
        String sql = "select * from business where businessId=?";
        System.out.println("执行的SQL: " + sql);
        try {
            con = DBUtil.getConnection();
            System.out.println("数据库连接成功");
            pst = con.prepareStatement(sql);
            pst.setInt(1, businessId);
            rs = pst.executeQuery();
            System.out.println("SQL执行成功，开始处理结果集");
            while (rs.next()) {
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                System.out.println("处理商家数据: " + business);
            }
            System.out.println("查询完成，商家信息: " + business);
        } catch (Exception e) {
            System.err.println("查询商家信息时发生异常: " + e.getMessage());
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs, pst);
            System.out.println("数据库资源已释放");
        }
        return business;
    }
}
